package mfuny.ggongal.admin.json;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import mfuny.ggongal.admin.Server;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ibatis.sqlmap.client.SqlMapClient;

@Controller
public class JsonCreater {
	
	SqlMapClient sqlMapper;
	
	public JsonCreater() throws Exception {
		sqlMapper = Server.getSqlMapper( );
	}
	
	//웹에서 제이슨 테스트.
	@RequestMapping("/jsonTest.ga")
	public ModelAndView jsonTest() throws Exception {
		return new ModelAndView("/test/jsonTest.ga");
	}
	
	//레코드1 객체 구하기.
	public Object getDao(String sqlId, Object param) throws Exception {
		Object dao = sqlMapper.queryForObject( sqlId, param );
		return dao;
	}
	
	//리스트 구하기.
	@SuppressWarnings( "unchecked" )
	public List<Object> getDaoList(String sqlId, Object param) throws Exception {
		List<Object> daoList = sqlMapper.queryForList( sqlId, param );
		return daoList;
	}
	
	//DAO JSON화.
	@SuppressWarnings( "unchecked" )
	public JSONObject getJson(Object targetObj) throws Exception {
		JSONObject json = new JSONObject();

		Class c = targetObj.getClass();
		Method[] m = c.getMethods();
		
		for ( int i = 0; i < m.length; i++ ) {
			if( !m[i].getName( ).substring( 0, 3 ).equals( "get" ) || m[i].getName( ).equals( "getClass" ) ) {
				continue;
			}
			json.put( m[i].getName( ).substring( 3 ).toLowerCase( ), m[i].invoke( targetObj ) );
		}
		
		return json;
	}
	
	//Map JSON화.
	@SuppressWarnings( "unchecked" )
	public JSONObject getJsonByMap(HashMap<String, Object> map) throws Exception {
		JSONObject json = new JSONObject();
		
		Iterator<Entry<String, Object>> iter = map.entrySet( ).iterator( );
		while( iter.hasNext( ) ) {
			Entry<String, Object> en = iter.next( );
			json.put( en.getKey( ), en.getValue( ) );
		}
		
		return json;
	}
	
	//DaoList JSONArray화.
	@SuppressWarnings( "unchecked" )
	public JSONObject getJsonByList(List<Object> list, String listName) throws Exception {
		JSONArray jsonArray = new JSONArray();
		
		for( int i = 0; i < list.size( ); i++ ) {
			Object obj = list.get( i );
			jsonArray.add( getJson( obj ) );
		}
		
		JSONObject json = new JSONObject();
		json.put( listName, jsonArray );
		
		return json;
	}
	
	//MapList JSONArray화.
	@SuppressWarnings( "unchecked" )
	public JSONObject getJsonByMapList(List<Object> list, String listName) throws Exception {
		JSONArray jsonArray = new JSONArray();
		
		for( int i = 0; i < list.size( ); i++ ) {
			HashMap<String, Object> map = (HashMap<String,Object>) list.get( i );
			jsonArray.add( getJsonByMap( map ) );
		}
		
		JSONObject json = new JSONObject();
		json.put( listName, jsonArray );
		
		return json;
	}
	
	//리스트 내용이 없는 JSONArray 리턴(ad_no = 0 ).
	@SuppressWarnings( "unchecked" )
	public JSONObject getNullJsonList(String listName) throws Exception {
		JSONArray jsonArray = new JSONArray();
		JSONObject json = new JSONObject();
		JSONObject jsonParam = new JSONObject();
		jsonParam.put( "ad_no", 0 );
		jsonArray.add( jsonParam );
		json.put( listName, jsonArray );
		
		return json;
	}
	
	//문자열을 JSON 객제화.
	public JSONObject getJsonByString( String jsonString ) {
		JSONObject json = (JSONObject)JSONValue.parse( jsonString );
		return json;
	}
}
